package JUC_Advanced.chapter2_atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

public class LogKpiCountTest {
    public static void main(String[] args) {
        LogKpiCount count = new LogKpiCount();
        int size = 100;
        CountDownLatch latch1 = new CountDownLatch(size);
        CountDownLatch latch2 = new CountDownLatch(size);
        CountDownLatch latch3 = new CountDownLatch(size);
        CountDownLatch latch4 = new CountDownLatch(size);
        Long startTime=null;
        startTime=System.currentTimeMillis();
        for (int i=0;i<size;i++){
            new Thread(()->{
                try {
                    for (int j = 0; j < 1000000; j++) {
                        count.synchronizedAdd();
                    }
                }finally {
                    latch1.countDown();
                }

            }).start();
        }
        try {
            latch1.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("synchronizedAdd执行耗时："+(System.currentTimeMillis()-startTime)+",执行结果:"+count.num);

        startTime=System.currentTimeMillis();
        for (int i=0;i<size;i++){
            new Thread(()->{
                try {
                    for (int j = 0; j < 1000000; j++) {
                        count.atomicIntegerAdd();
                    }
                }finally {
                    latch2.countDown();
                }

            }).start();
        }
        try {
            latch2.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("atomicIntegerAdd执行耗时："+(System.currentTimeMillis()-startTime)+",执行结果:"+count.atomicInteger.get());

        startTime=System.currentTimeMillis();
        for (int i=0;i<size;i++){
            new Thread(()->{
                try {
                    for (int j = 0; j < 1000000; j++) {
                        count.longAdderAdd();
                    }
                }finally {
                    latch3.countDown();
                }

            }).start();
        }
        try {
            latch3.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("longAdderAdd执行耗时："+(System.currentTimeMillis()-startTime)+",执行结果:"+count.longAdder.sum());

        startTime=System.currentTimeMillis();
        for (int i=0;i<size;i++){
            new Thread(()->{
                try {
                    for (int j = 0; j < 1000000; j++) {
                        count.longAccumulatorAdd();
                    }
                }finally {
                    latch4.countDown();
                }

            }).start();
        }
        try {
            latch4.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("longAccumulatorAdd执行耗时："+(System.currentTimeMillis()-startTime)+",执行结果:"+count.accumulator.get());
    }

    static class LogKpiCount{
        int num;
        AtomicInteger atomicInteger = new AtomicInteger();
        LongAdder longAdder = new LongAdder();
        LongAccumulator accumulator = new LongAccumulator((x, y)->x+y,0);
        public synchronized void synchronizedAdd(){
            num++;
        }

        public void atomicIntegerAdd(){
            atomicInteger.getAndIncrement();
        }

        public void longAdderAdd(){
            longAdder.increment();
        }

        public void longAccumulatorAdd(){
            accumulator.accumulate(1);
        }
    }
}
